package com.tsing.bms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsing.bms.entity.Depot;
import com.tsing.bms.mapper.DepotMapper;
import com.tsing.bms.service.IDepotService;
import com.tsing.bms.vo.req.DepotAddReqVo;
import com.tsing.bms.vo.req.DepotUpdateReqVo;
import com.tsing.bms.vo.req.IdReqVo;
import com.tsing.bms.vo.req.IdsReqVo;
import com.tsing.bms.vo.resp.DepotRespVo;
import com.tsing.common.exceptions.BusinessException;
import com.tsing.common.exceptions.ServerException;
import com.tsing.common.utils.BeanCoperUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author TheTsing
 * @since 2021-03-29
 */
@Service
public class DepotServiceImpl extends ServiceImpl<DepotMapper, Depot> implements IDepotService {

    @Override
    public void addDepot(DepotAddReqVo reqVo) {
        if (0 != baseMapper.selectCount(new QueryWrapper<Depot>().eq("name", reqVo.getName()))) {
            throw new BusinessException(ServerException.Proxy.NAME_HAVE_RELEVANCE);
        }
        Depot depot = new Depot();
        BeanUtils.copyProperties(reqVo, depot);
        baseMapper.insert(depot);
    }

    @Override
    public void deleteDepot(IdsReqVo reqVo) {
        if (!StringUtils.isBlank(baseMapper.haveRelevance(reqVo.getIds()))) {
            throw new BusinessException(ServerException.Proxy.DEPOT_HAVE_RELEVANCE);
        }
        baseMapper.deleteBatchIds(reqVo.getIds());
    }

    @Override
    public DepotRespVo getDepotById(IdReqVo reqVo) {
        DepotRespVo depotRespVo = new DepotRespVo();
        BeanUtils.copyProperties(baseMapper.selectById(reqVo.getId()), depotRespVo);
        return depotRespVo;
    }

    @Override
    public void updateDepot(DepotUpdateReqVo reqVo) {
        if (0 != baseMapper.selectCount(new QueryWrapper<Depot>().eq("name", reqVo.getName())
                .ne("id", reqVo.getId()))) {
            throw new BusinessException(ServerException.Proxy.NAME_HAVE_RELEVANCE);
        }
        Depot depot = new Depot();
        BeanUtils.copyProperties(reqVo, depot);
        baseMapper.updateById(depot);
    }

    @Override
    public List<DepotRespVo> depotList() {
        List<DepotRespVo> depotRespVos = new ArrayList<>();
        try {
            depotRespVos = BeanCoperUtil.copyObjects(baseMapper.selectList(null), DepotRespVo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        depotRespVos.forEach(e -> {
            e.setCarportCount(baseMapper.findCarportCount(e.getId()));
            e.setCqyzCount(baseMapper.findCqyzCount(e.getId()));
            e.setCwzhCount(baseMapper.findCwzhCount(e.getId()));
            e.setXzcwCount(baseMapper.XzcwCount(e.getId()));
        });
        return depotRespVos;
    }
}
